<?php



class FP_File {

    protected $_MIME_TYPES = array( /* VIDEO FORMATS */                /* AUDIO FORMATS */		/* IMAGE FORMATS */
				    'qt' => 'video/quicktime',		'amr' => 'audio/amr',		'gif' => 'image/gif',
				    'mov' => 'video/quicktime',		'awb' => 'audio/awb',		'ief' => 'image/ief',
				    'mp4' => 'video/mp4',		'axa' => 'audio/annodex',	'jpeg' => 'image/jpeg',
				    'avi' => 'video/x-msvideo',		'au'  => 'audio/basic',		'jpg' => 'image/jpeg',
				    'mpg' => 'video/mpeg',		'snd' => 'audio/basic',		'jpe' => 'image/jpe',
				    'mpeg' => 'video/mpeg',		'flac' => 'audio/flac',		'pcx' => 'image/pcx',
				    'mpe' => 'video/mpeg',		'mid' => 'audio/midi',		'png' => 'image/png',
				    'flv' => 'video/x-flv',		'midi' => 'audio/midi',		'svg' => 'image/svg+xml',
				    'm4v' => 'video/mp4',		'kar' => 'audio/midi',		'svgz' => 'image/svg+xml',
				    '3gp' => 'video/3gpp',		'mpga' => 'audio/mpeg',		'tiff' => 'image/tiff',
				    'axv' => 'video/annodex',		'mpega' => 'audio/mpeg',	'tif' => 'image/tif',
				    'dl' => 'video/dl',			'mp2' => 'audio/mpeg',		'djvu' => 'image/vnd.djvu',
				    'dv' => 'video/dv',			'mp3' => 'audio/mpeg',		'djv' => 'image/vnd.djvu',
				    'fli' => 'video/fli',		'm4a' => 'audio/mpeg',		'wbmp' => 'image/vnd.wap.wbmp',
				    'ogv' => 'video/ogg',		'ogg' => 'audio/ogg',		'ras' => 'image/x-cmu-raster',
				    'lsf' => 'video/x-la-asf',		'oga' => 'audio/ogg',		'cdr' => 'image/x-coreldraw',
				    'lsx' => 'video/x-la-asf',		'spx' => 'audio/ogg',		'pat' => 'image/x-coreldrawpattern',
				    'mng' => 'video/x-mng',		'sid' => 'audio/prs.sid',	'cdt' => 'image/x-coreldrawtemplate',
				    'asf' => 'video/x-ms-asf',		'aif' => 'audio/x-aiff',	'cpt' => 'image/x-corelphotopaint',
				    'asx' => 'video/x-ms-asf',		'aiff' => 'audio/x-aiff',	'ico' => 'image/x-icon',
				    'wm' => 'video/x-ms-wm',		'aifc' => 'audio/x-aiff',	'art' => 'image/x-jg',
				    'wmv' => 'video/x-ms-wmv',		'gsm' => 'audio/x-gsm',		'jng' => 'image/x-jng',
				    'wmx' => 'video/x-ms-wmx',		'wma' => 'audio/x-ms-wma',	'bmp' => 'image/x-ms-bmp',
				    'wvx' => 'video/x-ms-wvx',		'wax' => 'audio/x-ms-wax',	'psd' => 'image/x-photoshop',
				    'movie' => 'video/x-sgi-movie',	'ra' => 'audio/x-pn-realaudio',	'pnm' => 'image/x-portable-anymap',
				    'mpv' => 'video/x-matroska',	'rm' => 'audio/x-pn-realaudio',	'pbm' => 'image/x-portable-bitmap',
				    'mkv' => 'video/x-matroska',	'ram' => 'audio/x-pn-realaudio','pgm' => 'image/x-portable-graymap',
									'wav' => 'audio/x-wav',		'ppm' => 'image/x-portable-pixmap',
													'rgb' => 'image/x-rgb',
													'xbm' => 'image/x-xbitmap',
													'xpm' => 'image/x-xpixmap',
													'xwd' => 'image/x-xwindowdump'
    );

    protected $_name = null;
    protected $_mimetype = null;
    protected $_path = null;

    public function __construct($pFileFieldName, $pDestination, $pNamePattern){
	$this->_receive($pFileFieldName, $pDestination, $pNamePattern);

	$path_parts = pathinfo($this->_path);
	$ext = $path_parts['extension'];
	if (array_key_exists($ext, $this->_MIME_TYPES))
	    $this->_mimetype = $this->_MIME_TYPES[$ext];

    }

    public function __get($pAtt){
	switch($pAtt){
	case 'name':
	return $this->_name;
	case 'mimetype':
	return $this->_mimetype;
	case 'path':
	return $this->_path;
	}
    }

    protected function generateFileName($pNamePattern, $pOriginalName){
	$fparts = pathinfo($pOriginalName);

	$name = $pNamePattern;
	$name = preg_replace('/%ext%/', $fparts['extension'], $name);
	$name = preg_replace('/%original%/', $fparts['filename'], $name);

	return $name;
    }

    protected function _receive($pFileFieldName, $pDestination, $pNamePattern){

	if (array_key_exists($pFileFieldName, $_FILES)){

	$f = $_FILES[$pFileFieldName];

	if ($f['error'] == UPLOAD_ERR_OK){

	    $fparts = pathinfo($f['name']);
	    $this->_type = $f['type'];
	    $this->_name = $this->generateFileName($pNamePattern, $fparts['basename']);
	    $this->_path = $pDestination . '/' . $this->_name;


	    $result = move_uploaded_file($f['tmp_name'], $this->_path);
	    if ($result === false)
	    throw new Exception('File upload error');
	} else {
	    switch ($f['error']){
	    case UPLOAD_ERR_INI_SIZE:
	    throw new Exception('File size exceeds maximum value allowed by php.ini', $f['error']);
	    case UPLOAD_ERR_FORM_SIZE:
	    throw new Exception('File size exceeds maximum value allowed by the form', $f['error']);
	    case UPLOAD_ERR_PARTIAL:
		throw new Exception('File did not upload completely', $f['error']);
	    case UPLOAD_ERR_NO_TMP_DIR:
	    throw new Exception('Invalid temporary folder', $f['error']);
	    case UPLOAD_ERR_CANT_WRITE:
	    throw new Exception('Insufficient permissions to write to temporary folder', $f['error']);
	    case UPLOAD_ERR_EXTENSION:
	    throw new Exception('File will not upload because of illegal extension', $f['error']);
	    case UPLOAD_ERR_NO_FILE:
	    throw new Exception('No file was uploaded', $f['error']);
	    }
	}
	} else
	throw new Exception('Invalid file field');
    }

    public function delete(){
	return unlink($this->path);
    }

    public static function Receive($pFileFieldName, $pDestination, $pNamePattern){

	try {
	$file = new FP_File($pFileFieldName, $pDestination, $pNamePattern);
	return $file;
	} catch (Exception $x){
	if ($x->getCode() == UPLOAD_ERR_NO_FILE)
	    return false;
	else
	    throw $x;
	}
    }
}

?>